package com.education.cms.service;

import com.education.cms.dao.ClockDao;
import com.education.cms.domain.Clock;
import com.education.cms.dto.clock.ComplexSearchDto;
import com.education.cms.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ClockService {
    @Autowired
    private ClockDao clockDao;

    /**
     * 获取登录用户还未签退的打卡
     */
    public Clock getMyClock(Integer userId){
        return clockDao.getClock(userId);
    }

    /**
     * 上课打卡
     */
    public void clockIn(Integer userId){
        Clock clock = new Clock();
        clock.setClockInTime(new Date());
        clock.setUserId(userId);
        clock.setClockStatus(0);
        clockDao.save(clock);
    }
    /**
     * 下课打卡
     */
    public String clockOut(Clock clock){
        Integer count = DateUtils.getMinuteBetween(clock.getClockInTime());
        if (count.equals(999)){
            clock.setClockStatus(8);
            clockDao.save(clock);
            return "您的打卡已逾期，已按早退处理！";
        }
        //此超时功能待正式上线开放
        /*else if (count < 90){
            return "您还未达到最短打卡时间！";
        }*/
        else {
            clock.setClockStatus(1);
            clock.setClockOutTime(new Date());
            clockDao.save(clock);
            return "下课打卡成功！";
        }
    }
    /**
     * 获取个人打卡分页列表
     */
    public Page<Clock> getMyClockList(Integer userId,Pageable pageable){
        return clockDao.findAllByUserId(userId,pageable);
    }

    /**
     * 根据日期和用户获取分页列表
     */
    public Page<Clock> getClockList(ComplexSearchDto searchDto,Pageable pageable){
        return clockDao.findAll(new Specification<Clock>() {
            @Override
            public Predicate toPredicate(Root<Clock> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> list = new ArrayList<>();
                if (searchDto.getUserId() != null){
                    list.add(criteriaBuilder.equal(root.get("userId").as(Integer.class),searchDto.getUserId()));
                }
                if (searchDto.getStatus() != null){
                    list.add(criteriaBuilder.equal(root.get("clockStatus").as(Integer.class),searchDto.getStatus()));
                }
                try {
                    if (searchDto.getBeginDate() != null && searchDto.getEndDate() != null){  //起始日期至结束日期之间的打卡列表
                        list.add(criteriaBuilder.between(root.get("clockInTime").as(Date.class),searchDto.getBeginDate(),searchDto.getEndDate()));
                    }else
                    if (searchDto.getBeginDate() == null && searchDto.getEndDate() != null){  //获取结束日期当天的打卡列表
                        list.add(criteriaBuilder.between(root.get("clockInTime").as(Date.class),DateUtils.dateNoTime(searchDto.getEndDate()),searchDto.getEndDate()));
                    }else
                    if (searchDto.getBeginDate() != null && searchDto.getEndDate() == null){  //获取起始日期至今的打卡列表
                        Date currentDate = DateUtils.dateFullTime(new Date());
                        list.add(criteriaBuilder.between(root.get("clockInTime").as(Date.class),searchDto.getBeginDate(),currentDate));
                    }
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                criteriaQuery.where(criteriaBuilder.and(list.toArray(new Predicate[list.size()])));
                return criteriaQuery.getRestriction();
            }
        },pageable);
    }
}
